<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="版本号" prop="versionNum">
        <el-input v-model="queryParams.versionNum" placeholder="请输入版本号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="升级类型" prop="remarkOne">
        <el-select clearable v-model="queryParams.remarkOne" placeholder="请选择类型">
          <el-option label="医生端apk" value="1"></el-option>
          <el-option label="患者端apk" value="2"></el-option>
          <el-option label="sys" value="3"></el-option>
          <el-option label="bin" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['kenuo:verison:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['kenuo:verison:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['kenuo:verison:remove']">删除</el-button>
      </el-col>
 <!--     <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['kenuo:verison:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="verisonList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="版本号" align="center" prop="versionNum" />
      <el-table-column label="版本说明" align="center" prop="versionDes" />
      <el-table-column label="升级连接" align="center" prop="url" />
      <el-table-column label="升级类型" align="center" prop="remarkOne" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.remarkOne==1">医生端apk</el-tag>
          <el-tag type="success" v-if="scope.row.remarkOne==2">患者端apk</el-tag>
          <el-tag type="warning" v-if="scope.row.remarkOne==3">sys</el-tag>
          <el-tag type="danger"  v-if="scope.row.remarkOne==4">bin</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="强制升级" align="center" prop="isForced" >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.isForced==1?"是":"否" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <!--     <el-table-column label="软件升级包存放位置" align="center" prop="url" />
      <el-table-column label="备用字段1" align="center" prop="remarkOne" />
      <el-table-column label="备用字段2" align="center" prop="remarkTwo" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['kenuo:verison:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['kenuo:verison:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open"  width="600px" append-to-body >

      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="版本号" prop="versionNum">
          <el-input v-model="form.versionNum" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="版本说明" prop="versionDes">
          <el-input type="textarea" maxlength="100" show-word-limit v-model="form.versionDes" placeholder="请输入升级说明" />

        </el-form-item>
        <el-form-item label="升级类型" prop="remarkOne">
          <el-select v-model="form.remarkOne" placeholder="请选择类型">
            <el-option label="医生端apk" value="1"></el-option>
            <el-option label="患者端apk" value="2"></el-option>
            <el-option label="sys" value="3"></el-option>
            <el-option label="bin" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="强制升级" prop="isForced">
          <el-select v-model="form.isForced" placeholder="请选择是否强制升级">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传软件包" prop="url">
         <UploadFiles :fileList="fileList" @operateFiles="operateFiles" :uploadType="uploadType" :des="des"></UploadFiles>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listVerison,
    getVerison,
    delVerison,
    addVerison,
    updateVerison,
  } from "@/api/kenuo/verison";
  import UploadFiles from '@/views/components/uploadFiles.vue'
  export default {
    name: "Verison",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        verisonList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          versionNum: null,
          versionDes: null,
          upLink: null,
          url: null,
          remarkOne: null,
          remarkTwo: null
        },
        //文件上传地址
        fileList: [],
       uploadType:'.apk',
        des:'注意：最多上传一个文件，文件大小不超过50M',
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          versionNum: [{
            required: true,
            message: "新版本号不能为空",
            trigger: "blur"
          }],
          versionDes: [{
            required: true,
            message: "版本说明不能为空",
            trigger: "blur"
          }],
          upLink: [{
            required: true,
            message: "升级连接不能为空",
            trigger: "blur"
          }],
          isForced: [{
            required: true,
            message: "请选择是否强制更新",
            trigger: "blur"
          }],
          remarkOne: [{
            required: true,
            message: "请选择类型",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    components:{
      UploadFiles
    },
    methods: {
      operateFiles(val){
        // console.log(val)
        this.fileList = val
      },
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        listVerison(this.queryParams).then(response => {
          this.verisonList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          versionNum: null,
          versionDes: null,
          upLink: null,
          createTime: null,
          updateTime: null,
          url: null,
          isForced:null,
          remarkOne: null,
          remarkTwo: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增";
        this.fileList = []
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getVerison(id).then(response => {
          this.form = response.data;
           this.form.isForced = String(this.form.isForced) //类型
          if(response.data.url){
            this.fileList = [{
              name: response.data.remarkTwo,
              url: response.data.url
            }]
          }else{
            this.fileList = []
          }

          this.open = true;
          this.title = "修改";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            // console.log(this.fileList)
            let obj = {
              ...this.form,
              remarkTwo: '',
              url: ''
            }

            if (this.fileList.length > 0) {
              obj.remarkTwo = this.fileList[0].name
              obj.url = this.fileList[0].url
            }else{
                this.$modal.msgWarning("请上传软件包");
              return
            }
            if (this.form.id != null) {
              updateVerison(obj).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addVerison(obj).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除该数据项？').then(function() {
          return delVerison(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      // /** 导出按钮操作 */
      // handleExport() {
      //   this.download('kenuo/verison/export', {
      //     ...this.queryParams
      //   }, `verison_${new Date().getTime()}.xlsx`)
      // }
    }
  };
</script>
